package com.jfm.jfmc.core.icc;

import com.jfm.jfmc.core.icc.worker.ResultCalculator;
import com.jfm.jfmm.collector.entity.collect.CollectParam;
import com.jfm.jfmm.collector.entity.collect.Param;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author DYang
 * @since 2024-10-17 23:09
 *
 * 采集任务统一管理
 */
@Data
@NoArgsConstructor
public class CollectTaskManage {

    /**
     * 采集数据缓存
     */
    @Getter
    private static Map<String,CollectParam> collectParamMap = null;

    /**
     *  初始化采集参数
     * @param collectParams
     */
    public static void init(List<CollectParam> collectParams) {
        if (collectParamMap == null) {
            collectParamMap = new ConcurrentHashMap<>(collectParams.size());
        }
        for (CollectParam collectParam : collectParams) {
            Param param = collectParam.getParam();
            String[] depends = param.getDepends();
            if (depends != null) {
                buildDependIds(param,collectParams);
            }
            collectParamMap.put(collectParam.getParam().getMetricId(), collectParam);
        }
        JobControl.init();
    }

    /**
     * 构建依赖ID
     * @param param
     * @param collectParams
     */
    private static void buildDependIds(Param param, List<CollectParam> collectParams) {
        String[] depends = param.getDepends();
        List<String> dependIds = new ArrayList<>();
        String metricId = param.getMetricId().substring(0,6);
        for (CollectParam collectParam : collectParams) {
            String code = collectParam.getParam().getMetricCode();
            String id = collectParam.getParam().getMetricId();
            if (id.contains(metricId)) {
                for (String depend : depends) {
                    if (depend.equals(code)) {
                        dependIds.add(id);
                        ResultCalculator.addDependId(id);
                        break;
                    }
                }
            }
        }
        param.setDependIds(dependIds.toArray(new String[0]));
    }


    /**
     * 添加采集任务参数
     * @param collectParam 采集任务参数
     */
    public static void addCollectParam(CollectParam collectParam){
        // 添加采集任务参数
        if (collectParamMap == null) {
            collectParamMap = new ConcurrentHashMap<>();
        }
        collectParamMap.put(collectParam.getParam().getMetricId(),collectParam);
    }

    /**
     * 获取采集任务参数
     * @param metricId
     * @return
     */
    public static CollectParam getCollectParam(String metricId) {
        if (collectParamMap == null) {
            return null;
        }
        return collectParamMap.get(metricId);
    }

    /**
     * 停止采集任务
     */
    public static void stopCollect(String metricId){
        CollectParam collectParam = collectParamMap.get(metricId);
        if(collectParam != null){
            collectParam.getParam().setEnabled(false);
        }
        JobControl.stopCollectWorker(metricId);
    }

    /**
     * 更新采集任务采集频率
     * @param metricId    采集任务ID
     * @param frequency 采集频率
     */
    public static void updateCollectFrequency(String metricId,int frequency){
        CollectParam collectParam = collectParamMap.get(metricId);
        if(collectParam != null){
            collectParam.getParam().setFrequency(frequency);
            // 更改控制器中的采集频率
            JobControl.updateCollectFrequency(metricId,frequency, Param.transformTimeUnit(collectParam.getParam().getTimeUnit()));
        }
    }
}
